package de.barny.main;

import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.LabelBuilder;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.LineBuilder;

public class TimeTable extends GridPane {

	int startHour = 9;
	int endHour = 19;
	int hourCount = endHour - startHour + 1;
	int hourLineDistance = 50;
	int leftMargin = 100;

	int tlWidth;
	int tlHeight;

	Pane tl;
	Line[] hours;
	Label[] hourNames;

	public TimeTable(int lM) {
		// leftMargin = lM;
		drawTimeTable();
	}

	private void drawTimeTable() {
		tl = new Pane();

		for (int i = 0; i <= hourCount; i++) {
			hours = new Line[i];
			hourNames = new Label[i];
		}

		for (int i = 0; i < hourCount; i++) {
			Line nLine = LineBuilder.create().startX((hourLineDistance) * i)
					.startY(20).endX((hourLineDistance) * i).endY(200)
					.stroke(Color.web("#DDD")).strokeWidth(1).build();
			hours[i] = nLine;
			Label nLabel = LabelBuilder.create().text((startHour + i) + ":00")
					.translateX(((hourLineDistance) * i)).textFill(Color.web("#999"))
					.build();
			hourNames[i] = nLabel;
		}
		tl.getChildren().addAll(hours);
		tl.getChildren().addAll(hourNames);
		this.add(tl, 1, 1);

		this.setPadding(new Insets(10, 20, 10, 30));
	}

	public void setTimeTableWidth(int w) {
		tlWidth = w - leftMargin;
		hourLineDistance = tlWidth / hourCount; // The one is representative for the strokes width
		updateTimeTable();
	}

	public void setTimeTableHeight(int h) {
		tlHeight = h;
		updateTimeTable();
	}

	private void updateTimeTable() {
		for (int i = 0; i < hourCount; i++) {
			hours[i].setStartX((hourLineDistance) * i);
			hours[i].setEndX((hourLineDistance) * i);
			hours[i].setEndY(tlHeight-30);
			
			hourNames[i].setTranslateX(((hourLineDistance) * i)-12);
		}
	}
	
	public int getHourCount() {
		return hourCount;
	}
	
	public int getHourDistance() {
		return hourLineDistance;
	}
	
	public Line[] getLines() {
		return hours;
	}
}
